/**
 * 
 */
package fctuc.dei.myTwitter.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import fctuc.dei.myTwitter.database.DbWrapper;
import fctuc.dei.myTwitter.pojo.User;

/**
 * @author brimbela
 *
 */
public class UserDAO implements IUserDAO {

	/**
	 * 
	 */
	String select;
	
	public UserDAO() {
	}


	@Override
	synchronized public User authenticateUser(User usr) {
		User user = null;
		select = "SELECT * FROM USERS WHERE USER_USERNAME = \""+usr.getUsername()+"\" AND USER_PASSWORD = \""+usr.getPassword()+"\"";
		ResultSet rs = DbWrapper.getInstance().executeSelect(select);
		try {
			if(rs.first()){
				user = this.getById(rs);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return user;
	}


	@Override
	synchronized public boolean follow(int segue, int seguido) {
		
		//TODO: validacoes
		select = "SELECT * FROM USER_SEGUE_USER " +
				"WHERE USER_ID_SEGUE = " + segue +
				" AND USER_ID_SEGUIDO = " + seguido;
		
		ResultSet rs = DbWrapper.getInstance().executeSelect(select);
		try {
			if(rs.first()){
				rs.close();
				return false;
			}
			else{
				select = "insert into user_segue_user (user_id_segue, user_id_seguido)" +
						"values ("+segue+","+seguido+")";
				DbWrapper.getInstance().executeNonSelect(select);
				rs.close();
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}


	@Override
	synchronized public ArrayList<User> getFollowers(User user) {
		ArrayList<User> followers = new ArrayList<User>();
		
		select = "SELECT user_id_segue, usrs.user_username, usrs.user_location, usrs.user_bio " +
				"FROM USER_SEGUE_USER usu, USERS usrs" +
				" WHERE user_id_seguido = " + this.getByUsername(user.getUsername()) +
				" AND usu.user_id_segue = usrs.user_id";
		
		ResultSet rs = DbWrapper.getInstance().executeSelect(select);
		
		try{
			while(rs.next()){
					User u = new User();
					u.setId(rs.getInt("user_id_segue"));
					u.setUsername(rs.getString("user_username"));
					u.setCity(rs.getNString("user_location"));
					u.setBio(rs.getNString("user_bio"));
					followers.add(u);
				}
			rs.close();
		}catch (SQLException e){
			e.printStackTrace();
		}
		
		return followers;
	}


	@Override
	synchronized public ArrayList<User> getFollowing(User user) {
		ArrayList<User> following = new ArrayList<User>();
		
		select = "SELECT user_id_seguido, usrs.user_username, usrs.user_location, usrs.user_bio " +
		"FROM USER_SEGUE_USER usu, USERS usrs" +
		" WHERE user_id_segue = " + user.getId() +
		" AND user_id_seguido = usrs.user_id";
		
		ResultSet rs = DbWrapper.getInstance().executeSelect(select);
		
		try{
			while(rs.next()){
					User u = new User();
					u.setUsername(rs.getString("user_username"));
					u.setCity(rs.getNString("user_location"));
					u.setBio(rs.getNString("user_bio"));
					following.add(u);
			}
			rs.close();
		}catch (SQLException e){
			e.printStackTrace();
		}
		
		return following;
	}
	
	@Override
	synchronized public int getByUsername(String username){
		int id = 0;
		select = "SELECT user_id FROM USERS WHERE USER_USERNAME = \""+username+"\"";
		ResultSet rs = DbWrapper.getInstance().executeSelect(select);
		try {
			if(!rs.first()){
				rs.close();
				return 0;
			}
			else
			{
				id = rs.getInt(rs.getRow());
				rs.close();
			}
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return id;
	}

	@Override
	synchronized public int registerUser(User newUser) {
		int id = 0;
		if(getByUsername(newUser.getUsername()) > 0)
			return 0;
		else{
			select = "insert into users (user_username, user_password, user_bio, user_location, user_url, user_email)"
				+ "values (\""+newUser.getUsername()+"\",\""
							 +newUser.getPassword()+"\",\""
							 +newUser.getBio()+"\",\""
							 +newUser.getLocation()+"\",\""
							 +newUser.getUrl()+"\",\""
							 +newUser.getEmail() + "\")";
			
			DbWrapper.getInstance().executeNonSelect(select);
			id = this.getByUsername(newUser.getUsername());
			return id;
		}
		
	}

	
	@Override
	synchronized public ArrayList<User> searchUser(String searchParams) {
		ArrayList<User> results = new ArrayList<User>();
		User usr;
		String select = "SELECT * FROM USERS " +
				"WHERE UPPER(USER_USERNAME) LIKE UPPER(\"%" + searchParams +"%\")";
		ResultSet rs = DbWrapper.getInstance().executeSelect(select);
		try {
			while(rs.next()){
				usr = getById(rs);
				if(usr.getUsername().length()!=0)
					results.add(usr);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return results;
	}


	@Override
	public void unFollow(User userAsking, User userToBeUnfollowed) {
		// TODO Auto-generated method stub

	}


	@Override
	public void updateUser(User newUser) {
		// TODO Auto-generated method stub

	}
	
	@Override
	synchronized public User getById(ResultSet r){
		User usr = new User();
		try {
			usr.setUsername(r.getString("user_username"));

			usr.setPassword(r.getString("user_password"));
			usr.setBio(r.getString("user_bio"));
			usr.setCity(r.getString("user_location"));
			usr.setEmail(r.getString("user_email"));
			usr.setUrl(r.getString("user_url"));
			usr.setId(r.getInt("user_id"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return usr;
	}

}
